package com.ymatou.userservice.testcase;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.operate.trading.api.parameter.OrderPayRequestBean;
import com.ymatou.operate.trading.api.service.OrderPayRequestCall;
import com.ymatou.userservice.parameter.PasswordRequestDtoBean;
import com.ymatou.userservice.service.PasswordRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.common.enums.PlatformEnum;
import com.ymttest.database.model.YmtUsers;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 移除认证 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_PasswordRequestDto {
	private static PasswordRequestDtoBean lab;
	private static PasswordRequestDtoCall lac;
	private static ULC201406Wapper userWapper;

	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("修改密码");
		userWapper = new ULC201406Wapper();
	}

	@Before
	public void caseUp() {
		lab = new PasswordRequestDtoBean();
		lac = new PasswordRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(lac.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	//Token数据有延迟，导致case不稳定
	public void Tc_PasswordRequestDto_001() {
		Logger.start(true,"修改登入密码成功");
		try {
			String ip=TestDataManager.getRandomIp();
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,ip);
			String userName = user.get("loginId").toString();
			Map m = AuthCallService.authLogin(userName, "123456","app",ip);
			Logger.comment("登入成功:" + m);
			String accessToken1 = m.get("accessToken").toString();
			lab.setAType("ByUserId");
			lab.setPasActionType("ModifyLoginPassword");
			lab.setUserId(user.get("userId").toString());
			lab.setNewPassword("654321");
			lac.setData(lab);
			Logger.comment("调用修改密码接口");
			Thread.sleep(1000);
			lac.callService();
			Map m2 = AuthCallService.authLogin(userName, "654321","app",ip);
			Logger.comment("修改密码后登入成功:" + m2);
			// 如果登入失败（修改密码失败），accessToken2获取时会抛空指针异常
			String accessToken2 = m2.get("accessToken").toString();
			Logger.verifyNotEquals(accessToken1, accessToken2, "验证修改登入密码前后accessToken不一致");
			Map sam1 = AuthCallService.signAuth(accessToken1,"0.0.0.0");
			Logger.verifyEquals(false, sam1.get("success"), "验证第一次的Token失效");
			Map sam2 = AuthCallService.signAuth(accessToken2,"0.0.0.0");
			Logger.verifyEquals(true, sam2.get("success"), "验证第二次的Token有效");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	// 支付密码需要调用userservice，需要业务验证
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_PasswordRequestDto_002() {
		Logger.start(true,"修改支付密码成功");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), TestDataManager.getPhoneNum(),TestDataManager.getRandomIp());
			String userName = user.get("loginId").toString();
			String ip = user.get("ip").toString();
			String userId = user.get("userId").toString();
			Date oldtime = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId)).getLastupdatetime();
			String newTradingpwd = "123abc";
			Map m = AuthCallService.authLogin(userName, "123456","app",ip);
			Logger.comment("登入成功:" + m);
			String accessToken1 = m.get("accessToken").toString();
			lab.setAType("ByUserId");
			lab.setPasActionType("ModifyTradingPassword");
			lab.setUserId(userId);
			lab.setNewPassword(newTradingpwd);
			lac.setData(lab);
			Logger.comment("调用修改密码接口");
			lac.callService();
			Map m2 =  AuthCallService.authLogin(userName, "123456","app",ip);
			Logger.comment("修改支付后登入成功:" + m2);
			// 如果登入失败（修改密码失败），accessToken2获取时会抛空指针异常
			String accessToken2 = m2.get("accessToken").toString();
			// 验证数据库Lastupdatetime
			YmtUsers users = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId));
			Logger.verifyEquals(true, users.getLastupdatetime().after(oldtime),
					"验证Lastupdatetime更新:" + users.getLastupdatetime());
			Thread.sleep(5000);
			// 支付验证充值密码是否有效
			OrderPayRequestBean orderpayrequestbean = new OrderPayRequestBean();
			OrderPayRequestCall orderpayrequestcall = new OrderPayRequestCall();
			Map map = new OrderCallService().createM2cOrder(user);
			int OrderId = (Integer) map.get("orderId");
			int UserId = (Integer) map.get("userId");
			int SellerId = (Integer) map.get("sellerId");
			int TradingId = (Integer) map.get("tradingId");
			BigDecimal totalPrice=new  BigDecimal((map.get("totalPrice").toString()));
			// 参数
			orderpayrequestbean.setCallBackUrl("www.baidu.com");
			orderpayrequestbean.setUserId(UserId);
			orderpayrequestbean.setUseAccountAmount(totalPrice);
			orderpayrequestbean.setTradingId(TradingId);
			Logger.comment("使用新的支付密码进行支付");
			orderpayrequestbean.setTradingPwd(newTradingpwd);

			orderpayrequestbean.setUserIp("127.0.0.10");
			orderpayrequestbean.setBankId("");
			orderpayrequestbean.setPayType("10");
			orderpayrequestbean.setUsePlatform(PlatformEnum.Shangou_WAP);
			orderpayrequestbean.setParamExt("");
			orderpayrequestbean.setMemo("备注");

			// 发送请求
			orderpayrequestcall.setData(orderpayrequestbean);
			orderpayrequestcall.callService();
			Logger.verifyEquals(ECode.SUCCESS, orderpayrequestcall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_PasswordRequestDto_003() {
		Logger.start(false,"修改交易密码，交易密码与登陆密码一样，失败");
		try {
			Logger.comment("前置条件 - 创建新用户，登陆密码为：123456");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			
			lab.setAType("ByUserId");
			lab.setPasActionType("ModifyTradingPassword");
			lab.setUserId(user.get("userId").toString());
			lab.setNewPassword("123456");
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals("402", lac.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", lac.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("DtoError", lac.getString("Result"), "验证Result");
			Logger.verifyEquals("交易密码与登录密码相同", lac.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_PasswordRequestDto_004() {
		Logger.start(false,"修改登录密码，登录密码与交易密码一样，失败");
		try {
			Logger.comment("前置条件 - 创建新用户，登陆密码为：123456");
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			
			Logger.comment("修改交易密码为456789");
			lab.setAType("ByUserId");
			lab.setPasActionType("ModifyTradingPassword");
			lab.setUserId(user.get("userId").toString());
			lab.setNewPassword("456789");
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals(ECode.SUCCESS, lac.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", lac.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("Success", lac.getString("Result"), "验证Result");
			ULC201406Bean userBean = userWapper.getULCInfoByUid((Integer)user.get("userId")); 
			Logger.verifyEquals("EAC2B1B51A8A1A8D0B431F7849349A1A", userBean.gettPas(), "验证交易密码修改成功");
			
			Logger.comment("修改登录密码为456789");
			lab = new PasswordRequestDtoBean();
			lac = new PasswordRequestDtoCall();
			lab.setAType("ByUserId");
			lab.setPasActionType("modifyLoginPassword");
			lab.setUserId(user.get("userId").toString());
			lab.setNewPassword("456789");
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals("401", lac.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", lac.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("DtoError", lac.getString("Result"), "验证Result");
			Logger.verifyEquals("登录密码与交易密码相同", lac.getString("LastErrorMessage"), "验证Msg信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PasswordRequestDto_005() {
		Logger.start(false,"登录密码为空，修改失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			
			lab.setAType("ByUserId");
			lab.setPasActionType("modifyLoginPassword");
			lab.setUserId(user.get("userId").toString());
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals("500", lac.getResponseStatus().get("ErrorCode").toString(), "验证Code");
			Logger.verifyEquals(true, lac.getResponseStatus().get("Message").toString().contains("newPas不能为空"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PasswordRequestDto_006() {
		Logger.start(false,"交易密码为空，修改失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			
			lab.setAType("ByUserId");
			lab.setPasActionType("ModifyTradingPassword");
			lab.setUserId(user.get("userId").toString());
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals("500", lac.getResponseStatus().get("ErrorCode").toString(), "验证Code");
			Logger.verifyEquals(true, lac.getResponseStatus().get("Message").toString().contains("newPas不能为空"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_PasswordRequestDto_007() {
		Logger.start(false,"登录密码长度小于6，修改失败");
		try {
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			
			lab.setAType("ByUserId");
			lab.setPasActionType("modifyLoginPassword");
			lab.setUserId(user.get("userId").toString());
			lab.setNewPassword("iuu2");
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals("6", lac.getString("ErrorCode"), "验证ErrorCode");
			Logger.verifyEquals("0", lac.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", lac.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("NewPasswordLenthLt6", lac.getString("Result"), "验证Result");
			Logger.verifyEquals("密码长度不能小于6", lac.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}